package leetcode.code;

import leetcode.IDebug;
import leetcode.helper.tree.ListNode;

public class Solution160 implements IDebug {

	public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
		int a = 0;
		ListNode p = headA;
		while (p != null) {
			p = p.next;
			a++;
		}
		p = headB;
		while (p != null) {
			p = p.next;
			a--;
		}
		ListNode q = null;
		if (a > 0) {
			p = headA;
			q = headB;
		} else {
			p = headB;
			q = headA;
		}
		a = Math.abs(a);
		while (a > 0) {
			p = p.next;
			a--;
		}
		while (p != q) {
			p = p.next;
			q = q.next;
		}
		return p;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
